package com.xuecheng.media.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.xuecheng.api.media.model.AliyunVodApi;
import com.xuecheng.api.media.model.aliyun.VodUploadRequest;
import com.xuecheng.api.media.model.aliyun.VodUploadToken;
import com.xuecheng.media.common.utils.AliyunVODUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <p></p>
 *
 * @Description:
 */
@Slf4j
@RestController
public class AliyunVodController implements AliyunVodApi {

    @Value("${aliyun.region}")
    private String region;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    @PostMapping("media/vod-token")
    public VodUploadToken generateUploadToken(@RequestBody VodUploadRequest uploadRequest) {

        // 创建请求对象
        VodUploadToken token =null;
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setFileName(uploadRequest.getFileName());
        request.setTitle(uploadRequest.getTitle());
        try {
            // 初始化客户端对象
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
            // 解析响应数据并上传凭证和地址
            CreateUploadVideoResponse response = client.getAcsResponse(request);
            token = new VodUploadToken();
            token.setVideoId(response.getVideoId());
            token.setUploadAddress(response.getUploadAddress());
            token.setUploadAuth(response.getUploadAuth());
            token.setRequestId(response.getRequestId());
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        return token;
    }


    // printStackTrace：将程序中的所有调用关系的栈信息进行打印-日志（日志文件-IO）
    // 用户大量请求
    // e.printStackTrace();  //丢工作

    // 异常信息的说明
    // 1.只将错误信息记录
    // 2.将错误信息隐藏并返回友好提示

    @GetMapping("media/refresh-vod-token/{videoId}")
    public VodUploadToken refreshUploadToken(@PathVariable String videoId) {
        VodUploadToken token =new VodUploadToken();
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        // PS: 只要和本项目中微服务之外的内容要产生交互，必须要进行try-catch处理
        //   交互方：aliyun（第三方服务）
        //   原因：网络（不稳定因素）
        try {
            // 初始化客户端对象
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
            // 解析响应数据并上传凭证和地址
            CreateUploadVideoResponse response = client.getAcsResponse(request);
            token.setVideoId(videoId);
            token.setUploadAddress(response.getUploadAddress());
            token.setUploadAuth(response.getUploadAuth());
            token.setRequestId(response.getRequestId());
        } catch (Exception e) {
            // printStackTrace：将程序中的所有调用关系的栈信息进行打印-日志（日志文件-IO）
            // 用户大量请求
            // e.printStackTrace();  //丢工作

            // 异常信息的说明
            // 1.只将错误信息记录
            // 2.将错误信息隐藏并返回友好提示
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        return token;
    }

}